home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / LIB / EXPATH.C < prev    next >
C/C++ Source or Header  |  1992-11-27  |  7KB  |  171 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    e x p a t h . c                                                 */
  3. /*                                                                    */
  4. /*    Path expansion functions for UUPC/extended                      */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*                    MS-DOS and OS/2 header files                    */
  9. /*--------------------------------------------------------------------*/
  10.  
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16.  
  17. #ifndef __GNUC__
  18. #include <direct.h>
  19. #endif
  20.  
  21. /*--------------------------------------------------------------------*/
  22. /*                     UUPC/extended header files                     */
  23. /*--------------------------------------------------------------------*/
  24.  
  25. #include "lib.h"
  26. #include "expath.h"
  27. #include "hlib.h"
  28. #include "hostable.h"
  29. #include "security.h"
  30. #include "usertabl.h"
  31.  
  32. /*--------------------------------------------------------------------*/
  33. /*   e x p a n d _ p a t  h                                           */
  34. /*                                                                    */
  35. /*   Expands ~, ~/ and relative paths                                 */
  36. /*--------------------------------------------------------------------*/
  37.  
  38. char *expand_path(char *path,          /* Input/output path name     */
  39.                   const char *cur_dir, /* Default directory path     */
  40.                   const char *home,    /* Default home directory     */
  41.                   const char *ftype )  /* Default extension          */
  42. {
  43.    char        *p, *fname;
  44.    char        save[FILENAME_MAX];
  45.    struct UserTable *userp;
  46.  
  47. /*--------------------------------------------------------------------*/
  48. /*                   Convert backslashes to slashes                   */
  49. /*--------------------------------------------------------------------*/
  50.  
  51.    p  = path;
  52.    while ((p = strchr(p,'\\')) != NULL)
  53.       *p++ = '/';
  54.  
  55. /*--------------------------------------------------------------------*/
  56. /*                 Add optional extension, if needed                  */
  57. /*--------------------------------------------------------------------*/
  58.  
  59.    if ( ftype != NULL )
  60.    {
  61.       p = strrchr(path,'/');  /* Get the last slash in name          */
  62.  
  63.       if ( p == NULL )        /* No slash?                           */
  64.          p = path;            /* Okay, look at entire name           */
  65.  
  66.       if ( strchr( p , '.') == NULL )  /* Does name have a period?   */
  67.          strcat( strcat(p, ".") ,ftype );
  68.                               /* No --> Add extension                */
  69.    } /* if ( ftype != NULL ) */
  70.  
  71. /*--------------------------------------------------------------------*/
  72. /*               If a fully qualified path name, return               */
  73. /*--------------------------------------------------------------------*/
  74.  
  75. #ifdef __GNUC__
  76.    if (*path == '/')
  77.       return path;            /* nothing to do */
  78. #endif
  79.  
  80.    if ((*path == '/') || (isalpha( *path ) && (path[1] == ':')))
  81.    {
  82. #ifdef __GNUC__
  83.       if (path[2] == '/')     /* Absolute path on drive?             */
  84.          return path;         /* Yes --> Leave it alone              */
  85.  
  86.       printf(0,"Relative path \"%s\" not supported in GNU C",
  87.                path);
  88.       return NULL;
  89. #else
  90.       strcpy( save, path );
  91.       p = _fullpath( path, save, sizeof save );
  92.  
  93.       while ((p = strchr(p,'\\')) != NULL)
  94.          *p++ = '/';
  95.  
  96.       return path;
  97. #endif
  98.  
  99.    } /* if */
  100.  
  101. /*--------------------------------------------------------------------*/
  102. /*            Try to translate the file as a home directory path      */
  103. /*--------------------------------------------------------------------*/
  104.  
  105.    p = path;                  /* Copy entire path                    */
  106.    strcpy(save, p);
  107.    if (save[0] == '~')  {
  108.       if (save[1] == '/')  {
  109.          strcpy(path, home);  /* Use home dir for this user          */
  110.          fname = save + 2;    /* Step past directory for simple name */
  111.       }
  112.       else  {
  113.          if ((fname = strchr(save + 1, '/')) == NULL)
  114.          {
  115.             printmsg(0,"expand_path: path \"%s\" illegal",p);
  116.             return NULL;
  117.          }
  118.  
  119. /*--------------------------------------------------------------------*/
  120. /*                Look in /etc/passwd for the user id                 */
  121. /*--------------------------------------------------------------------*/
  122.  
  123.          *fname++ = '\0';           /* End string, step past it */
  124.          userp = checkuser(save + 1);  /* Locate user id in table  */
  125.          if ( userp == BADUSER )    /* Invalid user id?         */
  126.          {                          /* Yes --> Dump in trash    */
  127.             printmsg(0,"expand_path: User \"%s\" is invalid", save + 1);
  128.             return NULL;
  129.          } /* if */
  130.          strcpy(path, userp->homedir);
  131.       } /* else */
  132.    } /* if (save[0] == '~')  */
  133.  
  134. /*--------------------------------------------------------------------*/
  135. /*    No user id appears in the path; just append the input data      */
  136. /*    to the current directory to convert the relative path to an     */
  137. /*    absolute path                                                   */
  138. /*--------------------------------------------------------------------*/
  139.  
  140.    else {
  141.          fname = save;              /* Give it the file name - 6/23/91  */
  142.          if ( cur_dir == NULL )
  143.             getcwd( path, FILENAME_MAX);
  144.          else if ( equal(cur_dir,"."))
  145.          {
  146.             strcpy( path, save );
  147.             return path;
  148.          }
  149.          else
  150.             strcpy( path, cur_dir );
  151.    } /* else */
  152.  
  153. /*--------------------------------------------------------------------*/
  154. /*             Normalize the path, and then add the name              */
  155. /*--------------------------------------------------------------------*/
  156.  
  157.    while ((p = strchr(p,'\\')) != NULL)
  158.       *p++ = '/';
  159.    if ( path[ strlen( path ) - 1 ] != '/' )
  160.       strcat( path, "/");
  161.    strlwr( path );            /* Can lower case path, but not the
  162.                                  name because name may be UNIX!      */
  163.    strcat( path, fname );
  164.  
  165. /*--------------------------------------------------------------------*/
  166. /*                       Return data to caller                        */
  167. /*--------------------------------------------------------------------*/
  168.  
  169.    return path;
  170. } /* expand_path */
  171.